package com.terios;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.terios.http.JsonAnalysis;
import com.weibo.net.WeiboException;

/**
 * Status.java 2012-10-2 下午4:09:18
 * 
 * @author Wang Tengrui
 */
public class Status extends JsonAnalysis implements Serializable{
	private static final long serialVersionUID = 9180272616586778082L;
	private Date createdAt; // 微博创建时间
	private long id; // 微博ID(int64)
	private long mid; // 微博MID(int64)
	private String text; // 微博内容
	private String source; // 微博来源
	private boolean isFavorited; // 是否已收藏，true：是，false：否
	private boolean isTruncated; // 是否被截断，true：是，false：否
	private long inReplyToStatusId; // 回复ID
	private long inReplyToUserId; // 回复人UID
	private String inReplyToScreenName; // 回复人昵称
	private String thumbnail_pic; // 微博内容中的图片的缩略地址,注：没有是不返回此字段
	private String bmiddle_pic; // 中型图片,注：没有是不返回此字段
	private String original_pic; // 原始图片,注：没有是不返回此字段
	private Status retweeted_status; // 转发的微博内容
	private int deleted;// 判断被转发的微博是否被删除 1：已被删除 0：未被删除
	private int reposts_count; // 转发数
	private int comments_count; // 评论数

	private User user;

	public Status(JSONObject statusJsonObject) throws WeiboException {
		super();
		inti(statusJsonObject);
	}

	public Status(String responseString) throws WeiboException {
		JSONObject statusJsonObject = getJSONObject(responseString);
		inti(statusJsonObject);
	}

	// 创建statuses数组中的一个status的对象,注：这个方法写的逻辑有问题，稍后修改
	/*
	 * public Status(JSONArray statusJsonArray) throws WeiboException { super();
	 * JSONObject statusJsonObject = null; try { statusJsonObject =
	 * statusJsonArray.getJSONObject(0); } catch (JSONException e) { // TODO
	 * Auto-generated catch block e.printStackTrace(); } inti(statusJsonObject);
	 * }
	 */

	public static List<Status> statusList(String responseString) throws WeiboException {
		JSONObject jsonObject = getJSONObject(responseString);
		JSONArray statusesJsonArray;
		try {
			statusesJsonArray = jsonObject.getJSONArray("statuses");
			return statusList(statusesJsonArray);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List<Status> statusList(JSONArray statusesJsonArray) throws WeiboException {
		try {
			int size = statusesJsonArray.length();
			List<Status> statuses = new ArrayList<Status>(size);
			for (int i = 0; i < size; i++) {
				statuses.add(new Status(statusesJsonArray.getJSONObject(i)));
			}
			return statuses;
		} catch (JSONException jsone) {
			throw new WeiboException(jsone);
		}
	}

	private void inti(JSONObject statusJsonObject) throws WeiboException {
		try {
			createdAt = parseDate(statusJsonObject.getString("created_at"), "EEE MMM dd HH:mm:ss z yyyy");
			id = getLong("id", statusJsonObject);
			mid = getLong("mid", statusJsonObject);
			text = statusJsonObject.getString("text");
			if (statusJsonObject.isNull("deleted")) {
				source = statusJsonObject.getString("source");
				isFavorited = getBoolean("favorited", statusJsonObject);
				isTruncated = getBoolean("truncated", statusJsonObject);
				inReplyToStatusId = getLong("in_reply_to_status_id", statusJsonObject);
				inReplyToUserId = getLong("in_reply_to_user_id", statusJsonObject);
				inReplyToScreenName = statusJsonObject.getString("in_reply_to_screen_name");
				if (!statusJsonObject.isNull("thumbnail_pic")) {
					thumbnail_pic = statusJsonObject.getString("thumbnail_pic");
				}
				if (!statusJsonObject.isNull("bmiddle_pic")) {
					bmiddle_pic = statusJsonObject.getString("bmiddle_pic");
				}
				if (!statusJsonObject.isNull("original_pic")) {
					original_pic = statusJsonObject.getString("original_pic");
				}
				if (!statusJsonObject.isNull("retweeted_status")) {
					retweeted_status = new Status(statusJsonObject.getJSONObject("retweeted_status"));
				}
				reposts_count = getInt("reposts_count", statusJsonObject);
				comments_count = getInt("comments_count", statusJsonObject);
				if (!statusJsonObject.isNull("user")) {
					user = new User(statusJsonObject.getJSONObject("user"));
				}
			} else {
				deleted = getInt("deleted", statusJsonObject);
			}

		} catch (JSONException jsone) {
			throw new WeiboException(jsone.getMessage() + ":" + statusJsonObject.toString(), jsone);
		}
	}

	public Date getCreatedAt() {
		return createdAt;
	}

	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getMid() {
		return mid;
	}

	public void setMid(long mid) {
		this.mid = mid;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public boolean isTruncated() {
		return isTruncated;
	}

	public void setTruncated(boolean isTruncated) {
		this.isTruncated = isTruncated;
	}

	public boolean isFavorited() {
		return isFavorited;
	}

	public void setFavorited(boolean isFavorited) {
		this.isFavorited = isFavorited;
	}

	public long getInReplyToStatusId() {
		return inReplyToStatusId;
	}

	public void setInReplyToStatusId(long inReplyToStatusId) {
		this.inReplyToStatusId = inReplyToStatusId;
	}

	public long getInReplyToUserId() {
		return inReplyToUserId;
	}

	public void setInReplyToUserId(long inReplyToUserId) {
		this.inReplyToUserId = inReplyToUserId;
	}

	public String getInReplyToScreenName() {
		return inReplyToScreenName;
	}

	public void setInReplyToScreenName(String inReplyToScreenName) {
		this.inReplyToScreenName = inReplyToScreenName;
	}

	public String getThumbnail_pic() {
		return thumbnail_pic;
	}

	public void setThumbnail_pic(String thumbnail_pic) {
		this.thumbnail_pic = thumbnail_pic;
	}

	public String getBmiddle_pic() {
		return bmiddle_pic;
	}

	public void setBmiddle_pic(String bmiddle_pic) {
		this.bmiddle_pic = bmiddle_pic;
	}

	public String getOriginal_pic() {
		return original_pic;
	}

	public void setOriginal_pic(String original_pic) {
		this.original_pic = original_pic;
	}

	public Status getRetweeted_status() {
		return retweeted_status;
	}

	public void setRetweeted_status(Status retweeted_status) {
		this.retweeted_status = retweeted_status;
	}

	public int getReposts_count() {
		return reposts_count;
	}

	public void setReposts_count(int reposts_count) {
		this.reposts_count = reposts_count;
	}

	public int getComments_count() {
		return comments_count;
	}

	public void setComments_count(int comments_count) {
		this.comments_count = comments_count;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public int getDeleted() {
		return deleted;
	}

	public void setDeleted(int deleted) {
		this.deleted = deleted;
	}

	@Override
	public String toString() {
		return text;
	}
}
